/**
 * 缩放调整 Composable
 * 提供响应式的缩放调整功能
 */

import { zoomAdjustment } from '~/utils/zoom-adjustment'

export const useZoomAdjustment = () => {
  const isZoomApplied = ref(false)
  const currentConfig = ref(zoomAdjustment.getConfig())

  /**
   * 初始化缩放调整
   */
  const initZoom = () => {
    if (process.client) {
      zoomAdjustment.init()
      isZoomApplied.value = true
      currentConfig.value = zoomAdjustment.getConfig()
      
      console.log('缩放调整已初始化')
    }
  }

  /**
   * 重置缩放
   */
  const resetZoom = () => {
    if (process.client) {
      zoomAdjustment.resetZoom()
      isZoomApplied.value = false
      currentConfig.value = null
      
      console.log('缩放已重置')
    }
  }

  /**
   * 手动应用缩放
   */
  const applyZoom = () => {
    if (process.client) {
      zoomAdjustment.applyZoom()
      currentConfig.value = zoomAdjustment.getConfig()
      
      console.log('缩放已手动应用')
    }
  }

  /**
   * 检查是否需要缩放
   */
  const needsZoom = computed(() => {
    if (!process.client) return false
    
    const width = window.innerWidth
    return width >= 2560 // 只有4K及以上屏幕需要缩放
  })

  /**
   * 获取当前缩放比例
   */
  const scaleRatio = computed(() => {
    return currentConfig.value?.scale || 1
  })

  /**
   * 获取当前最大宽度
   */
  const maxWidth = computed(() => {
    return currentConfig.value?.maxWidth || '100%'
  })

  return {
    isZoomApplied: readonly(isZoomApplied),
    currentConfig: readonly(currentConfig),
    needsZoom,
    scaleRatio,
    maxWidth,
    initZoom,
    resetZoom,
    applyZoom
  }
}
